<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */

namespace app\admin\controller;

use app\admin\logic\OrderLogic;
use think\AjaxPage;
use think\Page;
use think\Verify;
use think\Db;
use app\admin\logic\UsersLogic;
use think\Loader;

class User extends Base
{

    public function index()
    {
        return $this->fetch();
    }

    /**
     * 会员列表
     */
    public function ajaxindex()
    {
        // 搜索条件
        $condition = array();
        I('mobile') ? $condition['mobile'] = I('mobile') : false;
        I('email') ? $condition['email'] = I('email') : false;

        I('first_leader') && ($condition['first_leader'] = I('first_leader')); // 查看一级下线人有哪些
        I('second_leader') && ($condition['second_leader'] = I('second_leader')); // 查看二级下线人有哪些
        I('third_leader') && ($condition['third_leader'] = I('third_leader')); // 查看三级下线人有哪些
        $sort_order = I('order_by') . ' ' . I('sort');

        $model = M('users');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        //  搜索条件下 分页赋值
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }

        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $user_id_arr = get_arr_column($userList, 'user_id');
        if (!empty($user_id_arr)) {
            $first_leader = DB::query("select first_leader,count(1) as count  from __PREFIX__users where first_leader in(" . implode(',', $user_id_arr) . ")  group by first_leader");
            $first_leader = convert_arr_key($first_leader, 'first_leader');

            $second_leader = DB::query("select second_leader,count(1) as count  from __PREFIX__users where second_leader in(" . implode(',', $user_id_arr) . ")  group by second_leader");
            $second_leader = convert_arr_key($second_leader, 'second_leader');

            $third_leader = DB::query("select third_leader,count(1) as count  from __PREFIX__users where third_leader in(" . implode(',', $user_id_arr) . ")  group by third_leader");
            $third_leader = convert_arr_key($third_leader, 'third_leader');
        }

        $this->assign('first_leader', $first_leader);
        $this->assign('second_leader', $second_leader);
        $this->assign('third_leader', $third_leader);
        $show = $Page->show();
        $this->assign('userList', $userList);
        $this->assign('level', M('user_level')->getField('level_id,level_name'));
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('pager', $Page);
        return $this->fetch();
    }

    //分佣记录
    public function commission()
    {
        return $this->fetch();
    }

    //
    public function ajaxcommission()
    {
        // 搜索条件
        $condition = array();
        $condition['id'] = ['gt', 0];
        input('post.start_time') ? $start_time = input('post.start_time') : false;
        input('post.end_time/s') ? $end_time = input('post.end_time/s') : false;

        if (!empty($start_time) && !empty($end_time) && ($start_time < $end_time)) {
            $condition['time'] = ["between", [$start_time, $end_time]];
        }

        $sort_order = I('order_by') . ' ' . I('sort');

        $model = M('fenyong_log');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        //  搜索条件下 分页赋值
//        foreach ($condition as $key => $val) {
//            print_r($condition);return;
//            $Page->parameter[$key] = urlencode($val);
//        }

        $userList = $model
            ->alias('f')
            ->field('f.*,u.nickname as user_name,u1.nickname as nickname1,u2.nickname as nickname2,u3.nickname as nickname3,u4.nickname as nickname4,u5.nickname as nickname5,u6.nickname as nickname6,u7.nickname as nickname7')
            ->join('__USERS__ u', 'u.user_id = f.user_id', 'LEFT')
            ->join('__USERS__ u1', 'u1.user_id = f.agent_id1', 'LEFT')
            ->join('__USERS__ u2', 'u2.user_id = f.agent_id2', 'LEFT')
            ->join('__USERS__ u3', 'u3.user_id = f.agent_id3', 'LEFT')
            ->join('__USERS__ u4', 'u4.user_id = f.agent_id4', 'LEFT')
            ->join('__USERS__ u5', 'u5.user_id = f.agent_id5', 'LEFT')
            ->join('__USERS__ u6', 'u6.user_id = f.agent_id6', 'LEFT')
            ->join('__USERS__ u7', 'u7.user_id = f.agent_id7', 'LEFT')
            ->where($condition)
            ->order($sort_order)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $show = $Page->show();
        $this->assign('userList', $userList);
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('pager', $Page);

        $amount_agent1 = $model->where($condition)->sum('agent_money1');
        $amount_agent2 = $model->where($condition)->sum('agent_money2');
        $amount_agent3 = $model->where($condition)->sum('agent_money3');
        $amount_agent4 = $model->where($condition)->sum('agent_money4');
        $amount_agent5 = $model->where($condition)->sum('agent_money5');
        $amount_agent6 = $model->where($condition)->sum('agent_money6');
        $amount_agent7 = $model->where($condition)->sum('agent_money7');
        $amount_agent = $amount_agent1 + $amount_agent2 + $amount_agent3 + $amount_agent4 + $amount_agent5 + $amount_agent6 + $amount_agent7;
        $this->assign('amount_agent1', $amount_agent1);// 一级代理总分润
        $this->assign('amount_agent2', $amount_agent2);// 二级代理总分润
        $this->assign('amount_agent3', $amount_agent3);// 三级代理总分润
        $this->assign('amount_agent4', $amount_agent4);// 四级代理总分润
        $this->assign('amount_agent5', $amount_agent5);// 五级代理总分润
        $this->assign('amount_agent6', $amount_agent6);// 六级代理总分润
        $this->assign('amount_agent7', $amount_agent7);// 七级代理总分润
        $this->assign('amount_agent', $amount_agent);// 所有代理总分润

        return $this->fetch();
    }

    //导出分拥记录
    public function export_commission()
    {
        $model = M('fenyong_log');
        $condition = array();
        $condition['id'] = ['gt', 0];
        $amount_agent1 = $model->where($condition)->sum('agent_money1');
        $amount_agent2 = $model->where($condition)->sum('agent_money2');
        $amount_agent3 = $model->where($condition)->sum('agent_money3');
        $amount_agent4 = $model->where($condition)->sum('agent_money4');
        $amount_agent5 = $model->where($condition)->sum('agent_money5');
        $amount_agent6 = $model->where($condition)->sum('agent_money6');
        $amount_agent7 = $model->where($condition)->sum('agent_money7');
        $amount_agent = $amount_agent1 + $amount_agent2 + $amount_agent3 + $amount_agent4 + $amount_agent5 + $amount_agent6 + $amount_agent7;

        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">ID</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">订单ID</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="100">会员昵称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">订单金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">一级代理商</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">所得分润</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">二级代理商</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">所得分润</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">三级代理商</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">所得分润</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">四级代理商</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">所得分润</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">五级代理商</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">所得分润</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">六级代理商</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">所得分润</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">七级代理商</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">所得分润</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">介绍</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">分佣日期</td>';
        $strTable .= '</tr>';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;"> </td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;"> </td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="100"> </td>';
        $strTable .= '<td style="text-align:center;font-size:12px;color:red;" width="*">全部代理统计：'.$amount_agent.'</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">  </td>';
        $strTable .= '<td style="text-align:center;font-size:12px;color:red;" width="*">一级代理总分润：'.$amount_agent1.'</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">  </td>';
        $strTable .= '<td style="text-align:center;font-size:12px;color:red;" width="*">二级代理总分润：'.$amount_agent2.' </td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*"></td>';
        $strTable .= '<td style="text-align:center;font-size:12px;color:red;" width="*">三级代理总分润：'.$amount_agent3.'</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*"></td>';
        $strTable .= '<td style="text-align:center;font-size:12px;color:red;" width="*">四级代理总分润：'.$amount_agent4.'</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*"></td>';
        $strTable .= '<td style="text-align:center;font-size:12px;color:red;" width="*">五级代理总分润：'.$amount_agent5.'</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*"></td>';
        $strTable .= '<td style="text-align:center;font-size:12px;color:red;" width="*">六级代理总分润：'.$amount_agent6.'</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*"></td>';
        $strTable .= '<td style="text-align:center;font-size:12px;color:red;" width="*">七级代理总分润：'.$amount_agent7.'</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*"></td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*"></td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*"> </td>';
        $strTable .= '</tr>';
        $count = M('fenyong_log')->count();
        $p = ceil($count / 5000);
        for ($i = 0; $i < $p; $i++) {
            $start = $i * 5000;
            $end = ($i + 1) * 5000;
            $userList = M('fenyong_log')
                ->alias('f')
                ->field('f.*,u.nickname as user_name,u1.nickname as nickname1,u2.nickname as nickname2,u3.nickname as nickname3,u4.nickname as nickname4,u5.nickname as nickname5,u6.nickname as nickname6,u7.nickname as nickname7')
                ->join('__USERS__ u', 'u.user_id = f.user_id', 'LEFT')
                ->join('__USERS__ u1', 'u1.user_id = f.agent_id1', 'LEFT')
                ->join('__USERS__ u2', 'u2.user_id = f.agent_id2', 'LEFT')
                ->join('__USERS__ u3', 'u3.user_id = f.agent_id3', 'LEFT')
                ->join('__USERS__ u4', 'u4.user_id = f.agent_id4', 'LEFT')
                ->join('__USERS__ u5', 'u5.user_id = f.agent_id5', 'LEFT')
                ->join('__USERS__ u6', 'u6.user_id = f.agent_id6', 'LEFT')
                ->join('__USERS__ u7', 'u7.user_id = f.agent_id7', 'LEFT')
                ->order('user_id')
                ->limit($start . ',' . $end)
                ->select();
            if (is_array($userList)) {
                foreach ($userList as $k => $val) {
                    $strTable .= '<tr>';
                    $strTable .= '<td style="text-align:center;font-size:12px;">' . $val['id'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['order_id'] . ' </td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['user_name'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['order_money'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['nickname1'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['agent_money1'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['nickname2'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['agent_money2'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['nickname3'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['agent_money3'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['nickname4'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['agent_money4'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['nickname5'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['agent_money5'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['nickname6'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['agent_money6'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['nickname7'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['agent_money7'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['content'] . ' </td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['time'] . ' </td>';
                    $strTable .= '</tr>';
                }
                unset($userList);
            }
        }
        $strTable .= '</table>';
        downloadExcel($strTable, 'fenyong_log_' . $i);
        exit();
    }

    /**
     * 会员详细信息查看
     */
    public function detail()
    {
        $uid = I('get.id');
        $user = D('users')->where(array('user_id' => $uid))->find();

        if (!$user) {
            exit($this->error('会员不存在'));
        }

        if (IS_POST) {
            //  会员信息编辑
            $password = I('post.password');
            $password2 = I('post.password2');
            $tnickname = I('post.tnickname');

            if ($_POST['level'] != $user['level']) {
                $da = $this->level_user($user, $_POST['level']);
                $_POST['user_amount'] = $da['user_amount'];
                $_POST['yx_amount'] = $da['yx_amount'];
                $_POST['amount'] = $da['amount'];
            }

            $nickname = M('users')->where(array("nickname" => $tnickname))->find();
            $_POST['first_leader'] = $nickname['user_id'];

            if ($password != '' && $password != $password2) {
                exit($this->error('两次输入密码不同'));
            }
            if ($password == '' && $password2 == '') {
                unset($_POST['password']);
            } else {
                $_POST['password'] = encrypt($_POST['password']);
            }

            if (!empty($_POST['email'])) {
                $email = trim($_POST['email']);
                $c = M('users')->where("user_id != $uid and email = '$email'")->count();
                $c && exit($this->error('邮箱不得和已有用户重复'));
            }

            if (!empty($_POST['mobile'])) {
                $mobile = trim($_POST['mobile']);
                $c = M('users')->where("user_id != $uid and mobile = '$mobile'")->count();
                $c && exit($this->error('手机号不得和已有用户重复'));
            }

            $row = M('users')->where(array('user_id' => $uid))->save($_POST);
            if ($row)
                exit($this->success('修改成功'));
            exit($this->error('未作内容修改或修改失败'));
        }

        $first_leader = M('users')->where(array('user_id' => $user['first_leader']))->find();
        $user['tnickname'] = $first_leader['nickname'];
        $user['first_lower'] = M('users')->where("first_leader = {$user['user_id']}")->count();
        $user['second_lower'] = M('users')->where("second_leader = {$user['user_id']}")->count();
        $user['third_lower'] = M('users')->where("third_leader = {$user['user_id']}")->count();


        $this->assign('user', $user);
        return $this->fetch();
    }

    public function level_user($user, $level)
    {
        if ($user) {
            $level = M('user_level')->where(array('level_id' => $level))->find();
            $data['user_amount'] = $level['user_amount'];
            $data['yx_amount'] = $level['yx_amount'];
            $data['amount'] = $level['amount'];
            // $row = M('users')->where(array('user_id'=>$user['user_id']))->save($data);
            return $data;
        }
    }

    public function add_user()
    {
        if (IS_POST) {
            $data = I('post.');
            $user_obj = new UsersLogic();
            $res = $user_obj->addUser($data);
            if ($res['status'] == 1) {
                $this->success('添加成功', U('User/index'));
                exit;
            } else {
                $this->error('添加失败,' . $res['msg'], U('User/index'));
            }
        }
        return $this->fetch();
    }

    public function export_user()
    {
        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">会员ID</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="100">会员昵称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">会员等级</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">手机号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">邮箱</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">注册时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">最后登陆</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">余额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">积分</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">累计消费</td>';
        $strTable .= '</tr>';
        $count = M('users')->count();
        $p = ceil($count / 5000);
        for ($i = 0; $i < $p; $i++) {
            $start = $i * 5000;
            $end = ($i + 1) * 5000;
            $userList = M('users')->order('user_id')->limit($start . ',' . $end)->select();
            if (is_array($userList)) {
                foreach ($userList as $k => $val) {
                    $strTable .= '<tr>';
                    $strTable .= '<td style="text-align:center;font-size:12px;">' . $val['user_id'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['nickname'] . ' </td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['level'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['mobile'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['email'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i', $val['reg_time']) . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i', $val['last_login']) . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['user_money'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['pay_points'] . ' </td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['total_amount'] . ' </td>';
                    $strTable .= '</tr>';
                }
                unset($userList);
            }
        }
        $strTable .= '</table>';
        downloadExcel($strTable, 'users_' . $i);
        exit();
    }

    /**
     * 用户收货地址查看
     */
    public function address()
    {
        $uid = I('get.id');
        $lists = D('user_address')->where(array('user_id' => $uid))->select();
        $regionList = get_region_list();
        $this->assign('regionList', $regionList);
        $this->assign('lists', $lists);
        return $this->fetch();
    }

    /**
     * 删除会员
     */
    public function delete()
    {
        $uid = I('get.id');
        $row = M('users')->where(array('user_id' => $uid))->delete();
        if ($row) {
            $this->success('成功删除会员');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 删除会员
     */
    public function ajax_delete()
    {
        $uid = I('id');
        if ($uid) {
            $row = M('users')->where(array('user_id' => $uid))->delete();
            if ($row !== false) {
                //把关联的第三方账号删除
                M('OauthUsers')->where(array('user_id' => $uid))->delete();
                $this->ajaxReturn(array('status' => 1, 'msg' => '删除成功', 'data' => ''));
            } else {
                $this->ajaxReturn(array('status' => 0, 'msg' => '删除失败', 'data' => ''));
            }
        } else {
            $this->ajaxReturn(array('status' => 0, 'msg' => '参数错误', 'data' => ''));
        }
    }

    /**
     * 账户资金记录
     */
    public function account_log()
    {
        $user_id = I('get.id');
        //获取类型
        $type = I('get.type');
        //获取记录总数
        $count = M('account_log')->where(array('user_id' => $user_id))->count();
        $page = new Page($count);
        $lists = M('account_log')->where(array('user_id' => $user_id))->order('change_time desc')->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign('user_id', $user_id);
        $this->assign('page', $page->show());
        $this->assign('lists', $lists);
        return $this->fetch();
    }

    /**
     * 账户资金调节
     */
    public function account_edit()
    {
        $user_id = I('user_id');
        if (!$user_id > 0) $this->ajaxReturn(['status' => 0, 'msg' => "参数有误"]);
        $user = M('users')->field('user_id,user_money,frozen_money,pay_points,is_lock')->where('user_id', $user_id)->find();
        if (IS_POST) {
            $desc = I('post.desc');
            if (!$desc)
                $this->ajaxReturn(['status' => 0, 'msg' => "请填写操作说明"]);
            //加减用户资金
            $m_op_type = I('post.money_act_type');
            $user_money = I('post.user_money/f');
            $user_money = $m_op_type ? $user_money : 0 - $user_money;
            //加减用户积分
            $p_op_type = I('post.point_act_type');
            $pay_points = I('post.pay_points/d');
            $pay_points = $p_op_type ? $pay_points : 0 - $pay_points;
            //加减冻结资金
            $f_op_type = I('post.frozen_act_type');
            $revision_frozen_money = I('post.frozen_money/f');
            if ($revision_frozen_money != 0) {    //有加减冻结资金的时候
                $frozen_money = $f_op_type ? $revision_frozen_money : 0 - $revision_frozen_money;
                $frozen_money = $user['frozen_money'] + $frozen_money;    //计算用户被冻结的资金
                if ($f_op_type == 1 and $revision_frozen_money > $user['user_money']) {
                    $this->ajaxReturn(['status' => 0, 'msg' => "用户剩余资金不足！！"]);
                }
                if ($f_op_type == 0 and $revision_frozen_money > $user['frozen_money']) {
                    $this->ajaxReturn(['status' => 0, 'msg' => "冻结的资金不足！！"]);
                }
                $user_money = $f_op_type ? 0 - $revision_frozen_money : $revision_frozen_money;    //计算用户剩余资金
                M('users')->where('user_id', $user_id)->update(['frozen_money' => $frozen_money]);
            }
            if (accountLog($user_id, $user_money, $pay_points, $desc, 0)) {
                $this->ajaxReturn(['status' => 1, 'msg' => "操作成功", 'url' => U("Admin/User/account_log", array('id' => $user_id))]);
            } else {
                $this->ajaxReturn(['status' => -1, 'msg' => "操作失败"]);
            }
            exit;
        }
        $this->assign('user_id', $user_id);
        $this->assign('user', $user);
        return $this->fetch();
    }

    public function recharge()
    {
        $timegap = urldecode(I('timegap'));
        $nickname = I('nickname');
        $map = array();
        if ($timegap) {
            $gap = explode(',', $timegap);
            $begin = $gap[0];
            $end = $gap[1];
            $map['ctime'] = array('between', array(strtotime($begin), strtotime($end)));
        }
        if ($nickname) {
            $map['nickname'] = array('like', "%$nickname%");
        }
        $count = M('recharge')->where($map)->count();
        $page = new Page($count);
        $lists = M('recharge')->where($map)->order('ctime desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('page', $page->show());
        $this->assign('pager', $page);
        $this->assign('lists', $lists);
        return $this->fetch();
    }

    public function level()
    {
        $act = I('get.act', 'add');
        $this->assign('act', $act);
        $level_id = I('get.level_id');
        if ($level_id) {
            $level_info = D('user_level')->where('level_id=' . $level_id)->find();
            $this->assign('info', $level_info);
        }
        return $this->fetch();
    }

    public function levelList()
    {
        $Ad = M('user_level');
        $p = $this->request->param('p');
        $res = $Ad->order('level_id')->page($p . ',10')->select();
        if ($res) {
            foreach ($res as $val) {
                $list[] = $val;
            }
        }
        $this->assign('list', $list);
        $count = $Ad->count();
        $Page = new Page($count, 10);
        $show = $Page->show();
        $this->assign('page', $show);
        return $this->fetch();
    }

    /**
     * 会员等级添加编辑删除
     */
    public function levelHandle()
    {
        $data = I('post.');
        $userLevelValidate = Loader::validate('UserLevel');
        $return = ['status' => 0, 'msg' => '参数错误', 'result' => ''];//初始化返回信息
        if ($data['act'] == 'add') {
            // if (!$userLevelValidate->batch()->check($data)) {
            //     $return = ['status' => 0, 'msg' => '添加失败', 'result' => $userLevelValidate->getError()];
            // } else {
            $r = D('user_level')->add($data);
            if ($r !== false) {
                $return = ['status' => 1, 'msg' => '添加成功', 'result' => $userLevelValidate->getError()];
            } else {
                $return = ['status' => 0, 'msg' => '添加失败，数据库未响应', 'result' => ''];
            }
            // }
        }
        if ($data['act'] == 'edit') {
            // if (!$userLevelValidate->scene('edit')->batch()->check($data)) {
            //     $return = ['status' => 0, 'msg' => '编辑失败', 'result' => $userLevelValidate->getError()];
            // } else {
            $r = D('user_level')->where('level_id=' . $data['level_id'])->save($data);
            if ($r !== false) {
                $discount = $data['discount'] / 100;
                D('users')->where(['level' => $data['level_id']])->save(['discount' => $discount]);
                $return = ['status' => 1, 'msg' => '编辑成功', 'result' => $userLevelValidate->getError()];
            } else {
                $return = ['status' => 0, 'msg' => '编辑失败，数据库未响应', 'result' => ''];
            }
            // }
        }
        if ($data['act'] == 'del') {
            $r = D('user_level')->where('level_id=' . $data['level_id'])->delete();
            if ($r !== false) {
                $return = ['status' => 1, 'msg' => '删除成功', 'result' => ''];
            } else {
                $return = ['status' => 0, 'msg' => '删除失败，数据库未响应', 'result' => ''];
            }
        }
        $this->ajaxReturn($return);
    }

    /**
     * 搜索用户名
     */
    public function search_user()
    {
        $search_key = trim(I('search_key'));
        if ($search_key == '') $this->ajaxReturn(['status' => -1, 'msg' => '请按要求输入！！']);
        $list = M('users')->where(['nickname' => ['like', "%$search_key%"]])->select();
        if ($list) {
            $this->ajaxReturn(['status' => 1, 'msg' => '获取成功', 'result' => $list]);
        }
        $this->ajaxReturn(['status' => -1, 'msg' => '未查询到相应数据！！']);
    }

    /**
     * 分销树状关系
     */
    public function ajax_distribut_tree()
    {
        $list = M('users')->where("first_leader = 1")->select();
        return $this->fetch();
    }

    /**
     *
     * @time 2016/08/31
     * @author dyr
     * 发送站内信
     */
    public function sendMessage()
    {
        $user_id_array = I('get.user_id_array');
        $users = array();
        if (!empty($user_id_array)) {
            $users = M('users')->field('user_id,nickname')->where(array('user_id' => array('IN', $user_id_array)))->select();
        }
        $this->assign('users', $users);
        return $this->fetch();
    }

    /**
     * 发送系统消息
     * @author dyr
     * @time  2016/09/01
     */
    public function doSendMessage()
    {
        $call_back = I('call_back');//回调方法
        $text = I('post.text');//内容
        $type = I('post.type', 0);//个体or全体
        $admin_id = session('admin_id');
        $users = I('post.user/a');//个体id
        $message = array(
            'admin_id' => $admin_id,
            'message' => $text,
            'category' => 0,
            'send_time' => time()
        );

        if ($type == 1) {
            //全体用户系统消息
            $message['type'] = 1;
            M('Message')->add($message);
        } else {
            //个体消息
            $message['type'] = 0;
            if (!empty($users)) {
                $create_message_id = M('Message')->add($message);
                foreach ($users as $key) {
                    M('user_message')->add(array('user_id' => $key, 'message_id' => $create_message_id, 'status' => 0, 'category' => 0));
                }
            }
        }
        echo "<script>parent.{$call_back}(1);</script>";
        exit();
    }

    /**
     *
     * @time 2016/09/03
     * @author dyr
     * 发送邮件
     */
    public function sendMail()
    {
        $user_id_array = I('get.user_id_array');
        $users = array();
        if (!empty($user_id_array)) {
            $user_where = array(
                'user_id' => array('IN', $user_id_array),
                'email' => array('neq', '')
            );
            $users = M('users')->field('user_id,nickname,email')->where($user_where)->select();
        }
        $this->assign('smtp', tpCache('smtp'));
        $this->assign('users', $users);
        return $this->fetch();
    }

    /**
     * 发送邮箱
     * @author dyr
     * @time  2016/09/03
     */
    public function doSendMail()
    {
        $call_back = I('call_back');//回调方法
        $message = I('post.text');//内容
        $title = I('post.title');//标题
        $users = I('post.user/a');
        $email = I('post.email');
        if (!empty($users)) {
            $user_id_array = implode(',', $users);
            $users = M('users')->field('email')->where(array('user_id' => array('IN', $user_id_array)))->select();
            $to = array();
            foreach ($users as $user) {
                if (check_email($user['email'])) {
                    $to[] = $user['email'];
                }
            }
            $res = send_email($to, $title, $message);
            echo "<script>parent.{$call_back}({$res['status']});</script>";
            exit();
        }
        if ($email) {
            $res = send_email($email, $title, $message);
            echo "<script>parent.{$call_back}({$res['status']});</script>";
            exit();
        }
    }

    /**
     * 提现申请记录
     */
    public function withdrawals()
    {
        $this->get_withdrawals_list();
        $this->assign('withdraw_status', C('WITHDRAW_STATUS'));
        return $this->fetch();
    }

    public function get_withdrawals_list($status = '')
    {
        $user_id = I('user_id/d');
        $realname = I('realname');
        $bank_card = I('bank_card');
        $create_time = I('create_time');
        $create_time = str_replace("+", " ", $create_time);
        $create_time2 = $create_time ? $create_time : date('Y-m-d', strtotime('-1 year')) . ' - ' . date('Y-m-d', strtotime('+1 day'));
        $create_time3 = explode(' - ', $create_time2);
        $this->assign('start_time', $create_time3[0]);
        $this->assign('end_time', $create_time3[1]);
        $where['w.create_time'] = array(array('gt', strtotime(strtotime($create_time3[0])), array('lt', strtotime($create_time3[1]))));
        $status = empty($status) ? I('status') : $status;
        if ($status !== '') {
            $where['w.status'] = $status;
        } else {
            $where['w.status'] = ['lt', 2];
        }
        $user_id && $where['u.user_id'] = $user_id;
        $realname && $where['w.realname'] = array('like', '%' . $realname . '%');
        $bank_card && $where['w.bank_card'] = array('like', '%' . $bank_card . '%');
        $export = I('export');
        if ($export == 1) {
            $strTable = '<table width="500" border="1">';
            $strTable .= '<tr>';
            $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">申请人</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="100">提现金额</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">银行名称</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">银行账号</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">开户人姓名</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">申请时间</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">提现备注</td>';
            $strTable .= '</tr>';
            $remittanceList = Db::name('withdrawals')->alias('w')->field('w.*,u.nickname')->join('__USERS__ u', 'u.user_id = w.user_id', 'INNER')->where($where)->order("w.id desc")->select();
            if (is_array($remittanceList)) {
                foreach ($remittanceList as $k => $val) {
                    $strTable .= '<tr>';
                    $strTable .= '<td style="text-align:center;font-size:12px;">' . $val['nickname'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['money'] . ' </td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['bank_name'] . '</td>';
                    $strTable .= '<td style="vnd.ms-excel.numberformat:@">' . $val['bank_card'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['realname'] . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i:s', $val['create_time']) . '</td>';
                    $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['remark'] . '</td>';
                    $strTable .= '</tr>';
                }
            }
            $strTable .= '</table>';
            unset($remittanceList);
            downloadExcel($strTable, 'remittance');
            exit();
        }
        $count = Db::name('withdrawals')->alias('w')->join('__USERS__ u', 'u.user_id = w.user_id', 'INNER')->where($where)->count();
        $Page = new Page($count, 20);
        $list = Db::name('withdrawals')->alias('w')->field('w.*,u.nickname')->join('__USERS__ u', 'u.user_id = w.user_id', 'INNER')->where($where)->order("w.id desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('create_time', $create_time2);
        $show = $Page->show();
        $this->assign('show', $show);
        $this->assign('list', $list);
        $this->assign('pager', $Page);
        C('TOKEN_ON', false);
    }

    /**
     * 删除申请记录
     */
    public function delWithdrawals()
    {
        $id = I('del_id/d');
        $res = Db::name("withdrawals")->where(['id' => $id])->delete();
        if ($res !== false) {
            $return_arr = ['status' => 1, 'msg' => '操作成功', 'data' => '',];
        } else {
            $return_arr = ['status' => -1, 'msg' => '删除失败', 'data' => '',];
        }
        $this->ajaxReturn($return_arr);
    }

    /**
     * 修改编辑 申请提现
     */
    public function editWithdrawals()
    {
        $id = I('id');
        $withdrawals = Db::name("withdrawals")->find($id);
        $user = M('users')->where(['user_id' => $withdrawals['user_id']])->find();
        if ($user['nickname'])
            $withdrawals['user_name'] = $user['nickname'];
        elseif ($user['email'])
            $withdrawals['user_name'] = $user['email'];
        elseif ($user['mobile'])
            $withdrawals['user_name'] = $user['mobile'];
        $status = $withdrawals['status'];
        $withdrawals['status_code'] = C('WITHDRAW_STATUS')["$status"];
        $this->assign('user', $user);
        $this->assign('data', $withdrawals);
        return $this->fetch();
    }

    /**
     *  处理会员提现申请
     */
    public function withdrawals_update()
    {
        $id_arr = I('id/a');
        $data['status'] = $status = I('status');
        $data['remark'] = I('remark');
        if ($status == 1) $data['check_time'] = time();
        if ($status != 1) $data['refuse_time'] = time();
        $ids = implode(',', $id_arr);
        $r = Db::name('withdrawals')->whereIn('id', $ids)->update($data);
        if ($r !== false) {
            $this->ajaxReturn(array('status' => 1, 'msg' => "操作成功"), 'JSON');
        } else {
            $this->ajaxReturn(array('status' => 0, 'msg' => "操作失败"), 'JSON');
        }
    }

    // 用户申请提现
    public function transfer()
    {
        $id = I('selected/a');
        if (empty($id)) $this->error('请至少选择一条记录');
        $atype = I('atype');
        if (is_array($id)) {
            $withdrawals = M('withdrawals')->where('id in (' . implode(',', $id) . ')')->select();
        } else {
            $withdrawals = M('withdrawals')->where(array('id' => $id))->select();
        }
        $alipay['batch_num'] = 0;
        $alipay['batch_fee'] = 0;
        foreach ($withdrawals as $val) {
            $user = M('users')->where(array('user_id' => $val['user_id']))->find();
            if ($user['user_money'] < $val['money']) {
                $data = array('status' => -2, 'remark' => '账户余额不足');
                M('withdrawals')->where(array('id' => $val['id']))->save($data);
                $this->error('账户余额不足');
            } else {
                $rdata = array('type' => 1, 'money' => $val['money'], 'log_type_id' => $val['id'], 'user_id' => $val['user_id']);
                if ($atype == 'online') {
                    header("Content-type: text/html; charset=utf-8");
                    exit("请联系TPshop官网客服购买高级版支持此功能");
                } else {
                    accountLog($val['user_id'], ($val['money'] * -1), 0, "管理员处理用户提现申请");//手动转账，默认视为已通过线下转方式处理了该笔提现申请
                    $r = M('withdrawals')->where(array('id' => $val['id']))->save(array('status' => 2, 'pay_time' => time()));
                    expenseLog($rdata);//支出记录日志
                }
            }
        }
        if ($alipay['batch_num'] > 0) {
            //支付宝在线批量付款
            include_once PLUGIN_PATH . "payment/alipay/alipay.class.php";
            $alipay_obj = new \alipay();
            $alipay_obj->transfer($alipay);
        }
        $this->success("操作成功!", U('remittance'), 3);
    }

    /**
     *  转账汇款记录
     */
    public function remittance()
    {
        $status = I('status', 1);
        $this->assign('status', $status);
        $this->get_withdrawals_list($status);
        return $this->fetch();
    }

    /**
     * 签到列表
     * @date 2017/09/28
     */
    public function signList()
    {
        header("Content-type: text/html; charset=utf-8");
        exit("暂无此功能");
    }


    /**
     * 会员签到 ajax
     * @date 2017/09/28
     */
    public function ajaxsignList()
    {
        header("Content-type: text/html; charset=utf-8");
        exit("暂无此功能");
    }

    /**
     * 签到规则设置
     * @date 2017/09/28
     */
    public function signRule()
    {
        header("Content-type: text/html; charset=utf-8");
        exit("暂无此功能");
    }
}